﻿using System;
using System.ServiceModel;
using ServiceProxy;

namespace CalculatorOne
{
    /// <summary>
    /// Implementation of a calculator which provides simple arithmetic operations
    /// </summary>
    public class Calculator
    {
        /* Dependency with WCF cannot be mocked, so unit testing is not possible
         * Only possible to test this against a (real) service
         * Also a lot of exception handling code is used which makes the code not clean*/

        /// <summary>
        /// Adds two numbers and returns the result
        /// </summary>
        /// <param name="numberOne"></param>
        /// <param name="numberTwo"></param>
        /// <returns></returns>
        public float Add(float numberOne, float numberTwo)
        {
            float response = 0f;
            var calculatorClient = new CalculatorClient();

            //Do not use the using statement when working with a WCF client: http://msdn.microsoft.com/en-us/library/aa355056.aspx
            try
            {
                response = calculatorClient.add(numberOne, numberTwo);
                calculatorClient.Close();
            }
            catch (CommunicationException)
            {
                calculatorClient.Abort();
            }
            catch (TimeoutException)
            {
                calculatorClient.Abort();
            }
            catch (Exception)
            {
                calculatorClient.Abort();
                throw;
            }

            return response;
        }
    }
}